<?php

namespace app\api\controller\v1;

use app\admin\model\kaoshi\examination\KaoshiUserExams;
use app\admin\model\kaoshi\examination\KaoshiUserPlan;
use app\common\controller\Api;
use app\common\logic\ExamLogic;
use app\common\utils\Utils;
use think\Db;
use think\Exception;


/**
 * 考试接口
 * 第一步 第一次进入考试 随机这个人所有考试的题目和答案，考试的题目传给前段
 *       第二次进入考试 获取考试到那道题目了，以及考试的答案
 *
 *
 */
class Exam extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    /**
    * 获取需要考试的试卷
     */
    public function getPlanExam() {
        $plan_map = [
            'userplan.user_id'         => UID,
            'plan.deletetime' => null,
            'plan.starttime'  => ['<', time()],
            'plan.endtime'    => ['>', time()],
        ];
        $userPlan = new KaoshiUserPlan();
        $list = $userPlan->alias('userplan')
            ->join('__KAOSHI_PLAN__ plan', 'userplan.plan_id = plan.id')
            ->where($plan_map)
            ->order('plan.createtime desc')
            ->field(['userplan.id', 'userplan.plan_id', 'plan.plan_name', 'plan.hours', 'plan.starttime', 'plan.endtime'])
            ->paginate()
            ->each(function ($item, $key) {
                $item->starttime_text = Utils::time_format($item->starttime, 'Y/m/d');
                $item->endtime_text = Utils::time_format($item->endtime, 'Y/m/d');
                $type = '';
                $btn = '';
                //是否开始了考试
                $userKaoshi = KaoshiUserExams::get(['user_plan_id' => $item->id]);
                if (!$userKaoshi) {
                    $item->is_start = -2;  //未开始
                } else {
                    $item->is_start = $userKaoshi['status'];
                }
                //判断考试是否结束
                if (time() < $item->starttime) {
                    $type = '未开始';  //开始未开始
                } else if ($item->starttime <= time() && time() <=$item->endtime) {
                    $type = '进行中';  //进行中
                    if (!$userKaoshi) {
                        $item->is_start = -1;  //开始答题
                    }
                } else {
                    $item->is_start = 2; //考试结束
                    $type = '已结束'; //已结束
                }

                //如果is_start为0那么判断是否超时间
                if ($item->is_start == 0) {
                    $time = ExamLogic::getTime($item->hours*60, $userKaoshi['starttime'], $item['endtime']);
                    if ($time >= $item->hours*60) {
                        $item->is_start = 3;
                    }
                }
                //is_start  -2 ：未开始 ，-1：开始答题，0：继续答题 ，1=答题完成 2：考试结束 3:考试未完成 时间超时了
                $item->type_kaoshi = $type;

            });

        $this->success('考试列表', $list);
    }
    /**
     * 试卷题目
     * @param int $user_plan_id 计划id
     * @throws Exception
     * @return array
     */
    public function getQuestion()
    {
        
        $user_plan_id = input('user_plan_id/d', 0);
        if (!$user_plan_id) {
            $this->error('参数错误');
        }
        $examLogic = new ExamLogic();
        $questions = $examLogic->setUserPlanId($user_plan_id)->getQuestions()->apiData();


        $this->success('试题信息', [
            'questions' => $questions,
            'user_exam_id' => $examLogic->userExamRow['id'],
            'cur_index'    => $examLogic->curIndex,
            'time'      => $examLogic->time
        ]);
    }


    /**
     * 回答题目
     * @param int $user_plan_id 计划id
     * @throws Exception
     * @return array
     */
    public function answer()
    {
        $id = input('user_exam_id/d', 0);    //指的是用考题id
        $timu_info = input('timu_info/a', []);
        $user_answer = input('user_answer/a', []);

        $examLogic = new ExamLogic();
        $res = $examLogic->save($id, UID, $timu_info, $user_answer);

        $this->success('提交答题结果', $res);
    }


    public function score($id = null) {
        $examLogic = new ExamLogic();
        $res = $examLogic->score($id);

        $this->success('得分', $res);
    }


    //获取考试结果
    public function getRes() {
        $user_plan_id = input('user_plan_id/d', 0);

        if (!$user_plan_id) {
            $this->error('参数错误');
        }

        $examLogic = new ExamLogic();
        $res = $examLogic->setUserPlanId($user_plan_id)->getRes();

        $this->success('考试结果', $res);
    }
}
